package com.taiji.dianthus.controller.sms;

import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.domain.sms.SmsGroup;
import com.taiji.dianthus.service.sms.SmsGroupService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.util.IPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import static com.taiji.dianthus.common.Constant.*;

/**
 * @ClassName SmsGroupViewController
 * @Description
 * @Author H.M 用户群组功能
 * @Date 2020/7/15
 */
@RequestMapping(value = "/sms/group")
@Controller
public class SmsGroupViewController extends BasicController {

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private SmsGroupService groupService;


    /**
     * 群组列表页面
     * 群发页面-编辑群组跳转到群组列表页面 标志：flag(sendGroup)  短信主题：topic  短信内容：content
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/list")
    public String getGroupList(HttpServletRequest request, Model model, Integer pageSize, Integer currentPage, String groupName) {
        pageSize = getPageSize(pageSize);
        currentPage = getCurrentPage(currentPage);
        StringBuffer url = new StringBuffer("/sms/group/list?pagSize=10");
        if (currentPage != null) {
            model.addAttribute("currentPage", currentPage + 1);
        }
        if (!StringUtils.isEmpty(groupName)) {
            url.append("&groupName=" + groupName);
            model.addAttribute("groupName", groupName);
        }
        BusinessMessage message = groupService.list(currentPage, pageSize, groupName, currentUser().getId());
        model.addAttribute("data", message.getData());
        model.addAttribute("currentUser", currentUser());
        model.addAttribute("menus", listMenu());
        model.addAttribute("url", url);
        sysLogService.save(currentUser(), LOG_OPERATION_QUERY, IPUtil.getIpAddr(request),
                "查询群组列表", message.success ? 1 : 0, message.getMsg());
        return "/pages/sms/groupList";
    }

    /**
     * 跳转新增或编辑页面
     *
     * @return
     */
    @RequestMapping(value = "/add")
    public String addGroup(HttpServletRequest request, Model model, String id) {
        SmsGroup smsGroup = new SmsGroup();
        if (!StringUtils.isEmpty(id)) {
            smsGroup = groupService.getOneById(id);
        }
        model.addAttribute("smsGroup", smsGroup);
        model.addAttribute("currentUser", currentUser());
        model.addAttribute("menus", listMenu());
        sysLogService.save(currentUser(), LOG_OPERATION_QUERY,
                IPUtil.getIpAddr(request), "跳转新增或编辑群组页面", 1, "查询成功");
        return "/pages/sms/groupAdd";
    }

    @ResponseBody
    @RequestMapping("/checkGroupName")
    public BusinessMessage checkGroupName(HttpServletRequest request, String groupName, String groupId) {
        BusinessMessage message = groupService.checkGroupName(groupName, groupId, currentUser().getId());
        sysLogService.save(currentUser(), LOG_OPERATION_QUERY,
                IPUtil.getIpAddr(request), "检查群组名称是否已存在", message.success ? 1 : 0, message.getMsg());
        return message;
    }

    @ResponseBody
    @RequestMapping("/save")
    public BusinessMessage saveGroup(HttpServletRequest request, SmsGroup smsGroup) {
        smsGroup.setCreateUserId(currentUser().getId());
        BusinessMessage message = groupService.save(smsGroup);
        message.setUrl("/sms/group/list");
        sysLogService.save(currentUser(), LOG_OPERATION_SAVE,
                IPUtil.getIpAddr(request), "保存群组信息", message.success ? 1 : 0, message.getMsg());
        return message;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public BusinessMessage delete(HttpServletRequest request, String id) {
        BusinessMessage message = groupService.delete(id);
        sysLogService.save(currentUser(), LOG_OPERATION_DEL,
                IPUtil.getIpAddr(request), "群组删除", message.success ? 1 : 0, message.getMsg());
        return message;
    }

}
